package com.atguigu.lease.web.admin.controller.system;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService ;

    @Autowired
    private SystemPostService systemPostService;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        IPage<SystemUserItemVo> page = Page.of(current, size);
        systemUserService.pageSystemUserItemVoByQueryVo(page,queryVo);
        return Result.ok(page);
        /*
        IPage<SystemUser> page = Page.of(current, size);

        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        Optional.ofNullable(queryVo.getName()).ifPresent(name->lambdaQueryWrapper.like(SystemUser::getName,name));
        Optional.ofNullable(queryVo.getPhone()).ifPresent(phone->lambdaQueryWrapper.like(SystemUser::getPhone,phone));

        systemUserService.page(page,lambdaQueryWrapper);

        List<SystemUserItemVo> systemUserItemVoList = page.getRecords().stream().map(systemUser -> {
            SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
            BeanUtils.copyProperties(systemUser, systemUserItemVo);
            SystemPost systemPost = systemPostService.getById(systemUser.getPostId());
            systemUserItemVo.setPostName(systemPost.getName());
            return systemUserItemVo;
        }).collect(Collectors.toList());


        IPage<SystemUserItemVo> page2 = Page.of(current,size);
        //这句话的作用是给pages这个属性赋值，前端ElementPlus中的分页控件需要用pages这个key
        BeanUtils.copyProperties(page,page2);
        page2.setRecords(systemUserItemVoList);

        return Result.ok(page2);
        */
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        SystemUserItemVo systemUserItemVo = systemUserService.getSystemUserItemVoById(id);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        if(StringUtils.hasText(systemUser.getPassword())){
             systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }
        systemUserService.saveOrUpdate(systemUser);

        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername,username);
        return Result.ok(systemUserService.count(lambdaQueryWrapper)>0);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        LambdaUpdateWrapper<SystemUser> systemUserLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        systemUserLambdaUpdateWrapper.set(SystemUser::getStatus,status);
        systemUserLambdaUpdateWrapper.eq(SystemUser::getId,id);
        systemUserService.update(systemUserLambdaUpdateWrapper);
        return Result.ok();
    }
}
